package com.ryzx.common.response;

import feign.FeignException;
import jakarta.el.MethodNotFoundException;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.ValidationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.server.MethodNotAllowedException;

import java.util.Objects;

@RestControllerAdvice
public class BapGlobalExceptionHandler {

//    @ExceptionHandler(value = Exception.class)
//    public ResponseResultDTO<Object> errorHandler(Exception e) {
//        return  ResponseResultDTO.fail(e.getMessage());
//    }

    /**
     * @Description: 业务异常处理
     * @Param
     * @param e 抛出的业务异常
     * @Return com.ysricc.sms.common.dto.ResponseResultDTO<java.lang.Object>
     */
    @ExceptionHandler(BapException.class)
    public BapResult SMSExceptionHandler(BapException e){
        return e.getResponseBody();
    }

    /**
     * @Description: 参数异常
     * @Param
     * @param e
     * @Return com.ysricc.sms.common.dto.ResponseResultDTO<java.lang.Object>
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public BapResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        String exceptionMsg = Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
        return BapResult.fail(BapResultCode.PARAM_NOT_VALID, exceptionMsg);
    }

    /**
     * @Description: 参数校验异常
     * @Param
     * @param e ConstraintViolationException
     * @Return com.ysricc.sms.common.dto.ResponseResultDTO<java.lang.Object>
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public BapResult constraintViolationExceptionHandler(ConstraintViolationException e) {
        return BapResult.fail(BapResultCode.PARAM_NOT_VALID, e.getMessage());
    }

    /**
     * @Description: 参数校验异常
     * @Param
     * @param e ValidationException
     * @Return com.ysricc.sms.common.dto.ResponseResultDTO<java.lang.Object>
     */
    @ExceptionHandler(ValidationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public BapResult validationExceptionHandler(ValidationException e) {
        return BapResult.fail(BapResultCode.PARAM_NOT_VALID, e.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public BapResult httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e) {
        return BapResult.fail(BapResultCode.SERVER_ERROR,e.getMessage());
    }


    /**
     * @Description: 运行时异常处理
     * @Param
     * @param e RuntimeException
     * @Return com.ysricc.sms.common.dto.ResponseResultDTO<java.lang.Object>
     */
    @ExceptionHandler(RuntimeException.class)
    public BapResult runtimeExceptionHandler(RuntimeException e) {
        return BapResult.fail(BapResultCode.SERVER_ERROR,e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public BapResult exceptionHandler(Exception e){
        return BapResult.fail(BapResultCode.SERVER_ERROR,e.getMessage());
    }


}
